package com.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiku.common.ResultJson;
import com.tiku.controller.DTO.MenuDTO;
import com.tiku.entity.Menu;
import com.tiku.entity.ProductSpecifications;
import com.tiku.entity.UserQuestionLog;
import com.tiku.mapper.ChapterInfoMapper;
import com.tiku.mapper.ExamPaperMapper;
import com.tiku.mapper.ProductSpecificationsMapper;
import com.tiku.service.MenuService;
import com.tiku.mapper.MenuMapper;
import com.tiku.service.ProductSpecificationsService;
import com.tiku.utils.RedisCache;
import com.tiku.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 *
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
        implements MenuService {

    @Autowired
    private ProductSpecificationsMapper specificationsMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private ChapterInfoMapper chapterInfoMapper;

    @Autowired
    private ExamPaperMapper paperMapper;


    @Autowired
    private RedisCache redisCache;

    @Override
    public ResultJson columnList(String column) {

        ProductSpecifications productSpecifications = specificationsMapper.selectById(column);
        String categoryId = productSpecifications.getCategoryId();
        List<Menu> menus = menuMapper.selectList(new QueryWrapper<Menu>().select("DISTINCT * ").lambda().eq(Menu::getPsCategoryId, categoryId));
        Menu menu2 = null;
        for (Menu menu : menus) {
            if (menu.getName().equals("章节练习")||menu.getName().equals("习题集")) {
                menu2 = menu;
                break;
            }

        }
        menus.remove(menu2);

        Integer sum = menuMapper.selectSumColumn(menu2.getId());
        sum = sum == null ? 0 : sum;
        sum += paperMapper.selectSumColumn(menus);


        System.out.println(sum);
        MenuDTO menuDTO = new MenuDTO();

        menus.add(0, menu2);
        menuDTO.setMenu(menus);
        menuDTO.setTotal(sum);
        String userId = ThreadLocalUtil.getThreadLocal();

        //key : questionLog: + 用户id + 类目ID+ 菜单ID+ 章节ID + 子章节ID + 题目ID
        Collection<String> keys = redisCache.keys("questionLog:" + userId + column + "*");

        //已做题目
        menuDTO.setDoneCount(keys.size());
        Integer count=0;
        for (String key : keys) {
            UserQuestionLog userQuestionLog = redisCache.getCacheObject(key);
            if (userQuestionLog.getRightflag()==0) {
                count++;
            }
        }
        int i = calculateCorrectRate(keys.size(), count);
        menuDTO.setCorrectness(i);
        menuDTO.setErrCount(count);
        return ResultJson.success(menuDTO);
    }

    public int calculateCorrectRate(int totalQuestions, int wrongQuestions) {
        int correctQuestions = totalQuestions - wrongQuestions;
        double rate = (double) correctQuestions / totalQuestions * 100;
        return (int) (rate + 0.5);
    }
}




